//
//  PresentDetailsViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/5.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "PresentDetailsViewController.h"
#import "NetworkHandler.h"
#import "PresentDetailsCollectionViewCell.h"
#import "PresentDetailsModel.h"
#import "HotDetailsViewController.h"
#import "ToolModel.h"
#import "ToolDB.h"
#import "TAMacro.h"
@interface PresentDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray * array;
@property (nonatomic,strong)PresentDetailsCollectionViewCell *celll;

@end
@implementation PresentDetailsViewController
-(NSMutableArray *)array{
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
    
    [self foundCollectionView];
    [self mjRefresh];
    [self nightModel];
}
-(void)mjRefresh{
    [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/item_subcategories/%@/items?limit=20&offset=0",self.str]  BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"items"];
        for (NSDictionary *dic in arr) {
            PresentDetailsModel *model = [PresentDetailsModel modelWithDic:dic];
            [self.array addObject:model];
        }
        [self.collectionView reloadData];
    }];
}
-(void)foundCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2 - 15, SCREEN_HEIGHT /2.44);
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0 , 5, 5, 5);
    
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView jxl_setDayMode:^(UIView *view) {
        self.collectionView.backgroundColor = [UIColor colorWithWhite:0.924 alpha:1.000];
    } nightMode:^(UIView *view) {
        self.collectionView.backgroundColor = [UIColor grayColor];
    }];
    [self.collectionView registerClass:[PresentDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.view addSubview:self.collectionView];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PresentDetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    [cell jxl_setDayMode:^(UIView *view) {
        cell.backgroundColor =  [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        cell.backgroundColor =  [UIColor grayColor];
    }];
    PresentDetailsModel *model = self.array[indexPath.item];
    cell.presentDetailsModel = model;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.hidesBottomBarWhenPushed = YES;
    HotDetailsViewController *HotDetails = [[HotDetailsViewController alloc]init];
    // 属性传值第3步
    PresentDetailsModel *model = self.array[indexPath.item];
    HotDetails.str = model.Id;
    // 属性传值第3步
    self.hidesBottomBarWhenPushed = YES;
    ToolModel *tool = [[ToolModel alloc]init];
    tool.title = model.name;
    tool.cover_image_url = model.cover_image_url;
    HotDetails.ID = [NSString stringWithFormat:@"%ld",(long)model.Id];
    tool.Id = HotDetails.ID;
    HotDetails.str = model.Id;
    HotDetails.IMAGE = model.cover_image_url;
    HotDetails.name = model.name;
    [self.navigationController pushViewController:HotDetails animated:YES];
    self.hidesBottomBarWhenPushed = YES;
}
-(void)clickLeft:(UIBarButtonItem*)qwe {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)nightModel {
    //设置日间和夜间两张状态
    [self.view jxl_setDayMode:^(UIView *view) {
        //设置日间模式
        view.backgroundColor = [UIColor whiteColor];
        
    } nightMode:^(UIView *view) {
        view.backgroundColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
    [self.navigationController.navigationBar jxl_setDayMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarStyleDefault;
        bar.barTintColor = [UIColor colorWithRed:0.98 green:0.29 blue:0.31 alpha:1.00];
        //        bar.barTintColor = [UIColor redColor];
    } nightMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarMetricsDefault;
        bar.barTintColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];

}

@end
